import { Button, Flex, message, Table, type TableColumnsType } from "antd";
import { useEffect, useRef, useState } from "react";
import { columns } from "./statics";
import { getToken, runJobAll, runJobCollect, runJobCollectSingle, runJobLike, runJobLikeSingle, runJobView, runJobViewSingle, type DataItem } from "./api";
import UpdateDrawer from "./updateDrawer";



export default function TokenManager() {

  const [dataSource, setDataSource] = useState<DataItem[]>([]);
  const [tableColumns, setTableColumns] = useState<TableColumnsType>([]);
  const [messageApi] = message.useMessage();
  const $ref = useRef<any>(null)
  async function load() {
    const res: any = await getToken()
    setDataSource(res.message)
  }

  useEffect(() => {
    load()
  }, [])

  useEffect(() => {
    setTableColumns(columns({ handleEdit, handleView, handleLike, handleCollect }))
  }, [$ref])

  function handleCreate() {
    $ref.current.open()
  }

  function handleEdit(data: DataItem) {
    const correctPassword = '!qaz@wsx';
    const input = prompt('请输入编辑密码');
    if (input === correctPassword) {
      $ref.current.open(data);
    } else {
      alert('密码错误，无权限编辑');
    }
  }

  async function handleView(data: DataItem) {
    const articleId = prompt('请输入要阅读的文章ID');
    if (articleId) {
      const { message } = await runJobViewSingle(data.userName, Number(articleId))
      messageApi.info(message);
    }
  }

  async function handleLike(data: DataItem) {
    const articleId = prompt('请输入要点赞的文章ID');
    if (articleId) {
      const { message } = await runJobLikeSingle(data.userName, Number(articleId))
      messageApi.info(message);
    }
  }

  async function handleCollect(data: DataItem) {
    const articleId = prompt('请输入要收藏的文章ID');
    if (articleId) {
      const { message } = await runJobCollectSingle(data.userName, Number(articleId))
      messageApi.info(message);
    }
  }

  async function handleDoJob() {
    const { message } = await runJobAll()
    messageApi.info(message);
  }

  async function handleViewAll() {
    const articleId = prompt('请输入要阅读的文章ID');
    if (articleId) {
      const { message } = await runJobView(Number(articleId))
      messageApi.info(message);
    }
  }
  async function handleLikeAll() {
    const articleId = prompt('请输入要点赞的文章ID');
    if (articleId) {
      const { message } = await runJobLike(Number(articleId))
      messageApi.info(message);
    }
  }
  async function handleCollectAll() {
    const articleId = prompt('请输入要收藏的文章ID');
    if (articleId) {
      const { message } = await runJobCollect(Number(articleId))
      messageApi.info(message);
    }
  }

  return <>
    <Flex vertical gap={16}>
      <Flex gap={8}>
        <Button type="primary" onClick={handleCreate}>新增</Button>
        <Button type="link" onClick={handleDoJob}>一键刷分</Button>
        <Button type="link" onClick={handleViewAll}>一键阅读</Button>
        <Button type="link" onClick={handleLikeAll}>一键点赞</Button>
        <Button type="link" onClick={handleCollectAll}>一键收藏</Button>
      </Flex>
      <Table rowKey="userName" dataSource={dataSource} columns={tableColumns} scroll={{ x: 'max-content' }} bordered />
    </Flex>
    <UpdateDrawer onOk={load} ref={$ref}></UpdateDrawer>
  </>
}
